home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_03 / letters / heap.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  1.0 KB  |  39 lines

  1. const unsigned BLOCKSIZE = 60*1024;
  2. const int MAXBLOCK = 64;
  3.  
  4. class Heap{
  5.   void far* block[MAXBLOCK] // Array of far
  6.                             // pointers to 
  7.                             // memory blocks
  8.   int n;                    // No. of blocks 
  9.                             // in use
  10.   unsigned pos;             // Next vacant
  11.                             // position within
  12.                             // current block
  13.  
  14.   public:
  15.   Heap()
  16.     { n = 0;
  17.     }
  18.   ~Heap()
  19.     { for (int i = 0; i<n; i++)
  20.         GlobalFreePtr(block[i]);
  21.     }
  22.   void far* New(unsigned nBytes)
  23.     {
  24.       // Allocate another block if necessary
  25.       if (n == 0 || nBytes > BLOCKSIZE - pos){
  26.         assert(n < MAXBLOCK && nBytes <= BLOCKSIZE);
  27.         block[n++] = GlobalAllocPtr(GHND, BLOCKSIZE);
  28.         assert(block[n-1] != 0);
  29.         pos = 0;
  30.       }
  31.       // Get a pointer from within the current<%0> block
  32.       void far* p =
  33.           ((char far*) block[n-1]) + pos;
  34.       pos += nBytes;
  35.       return p;
  36.     }
  37. };
  38.  
  39.